package com.yeheng;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * @Classname CopyFile
 * @Description :
 * @Date 2021/3/6 9:39
 * @Created by Yeheng
 */
class Copyer implements Runnable {

    private LinkedList<File> list;
    private String oldPath;
    private String newPath;

    public Copyer(LinkedList<File> list, String oldPath, String newPath) {
        this.list = list;
        this.oldPath = oldPath;
        this.newPath = newPath;
    }

    @Override
    public void run() {
        while (list.size() > 0) {
            File file = null;
            synchronized (list) {
                //第二遍是防止进来之后size已经为0
                if (list.size() > 0) {
                    file = list.poll();

                }
                //通知其他线程起来干活
                list.notifyAll();
            }
            if (file==null){
                return;
            }
            FileInputStream in = null;
            FileOutputStream out = null;
            try {
                //创建新文件对象
                File newFile = new File(file.getAbsolutePath().replace(oldPath, newPath));
                //确保父级目录存在
                File file1 = newFile.getParentFile();
                file1.mkdirs();
                //又读又写
                in = new FileInputStream(file);
                out = new FileOutputStream(newFile);
                //打印信息
                System.out.println(Thread.currentThread().getName() + "正在复制" + file.getName());
                while (in.available() > 0) {
                    byte[] bytes = new byte[1024];
                    in.read(bytes);
                    out.write(bytes);
                    out.flush();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }



        }
    }
}

public class CopyFile {
    public static void main(String[] args) {
        LinkedList<File> list = new LinkedList<>();


        String oldPath = "E:\\国庆资料";
        //来个新文件夹
        File file = new File("D:\\abc3");
        //确保父级存在
        file.mkdirs();

        Copyer copyer = new Copyer(list, oldPath, file.getAbsolutePath());
        //第一个oldPath是递归的起点
        getAllFile(list, oldPath, oldPath, file.getAbsolutePath());

        Thread thread = new Thread(copyer);
        Thread thread2 = new Thread(copyer);
        Thread thread3 = new Thread(copyer);
        Thread thread4 = new Thread(copyer);
        Thread thread5 = new Thread(copyer);
        thread.start();
        thread2.start();
        thread3.start();
        thread4.start();
        thread5.start();
    }

    public static void getAllFile(List<File> list, String path, String oldPath, String newPath) {
        File file = new File(path);
        File[] files = file.listFiles(e -> {
            if (e.isDirectory()) {
                getAllFile(list, e.getAbsolutePath(), oldPath, newPath);
                return false;
            } else {
                return true;
            }
        });
        list.addAll(Arrays.asList(files));

    }

}

